import { Ckeditor } from '@ckeditor/ckeditor5-vue'
import './assets/styles/global.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'

import App from './App.vue'
import router from './router'

import i18n, { getElementPlusLocale, getElementPlusI18nT } from './locales/i18n'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import './assets/styles/ck.css'
import 'moment/dist/locale/zh-hk'

const app = createApp(App)

app.use(i18n)
app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
    locale: getElementPlusLocale(),
    // 确保 Element Plus 使用 vue-i18n 的 t 函数
    i18n: getElementPlusI18nT(),
})

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.component('CKEditor', Ckeditor) // 全局注册组件

app.mount('#app')

router.afterEach(to => {
    let pageTitle: string = to.meta.title as string
    if (!pageTitle && to.meta.breadcrumb && to.meta.breadcrumb.length > 0) {
        const titleKey = to.meta.breadcrumb[to.meta.breadcrumb.length - 1].titleKey
        if (titleKey) {
            pageTitle = i18n.global.t(titleKey)
        }
    }

    document.title = pageTitle ? '香港教育大學 - ' + pageTitle : '香港教育大學'
})
